package cn.org.qary.controller.abelianymph.front;

import cn.org.qary.entity.FirstType;
import cn.org.qary.entity.SecondaryType;
import cn.org.qary.service.trait.yaokaiing.FirstTypeService;
import cn.org.qary.service.trait.yaokaiing.SecondaryTypeService;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import io.vavr.Tuple2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.stream.Collectors;

@Controller
@RequestMapping("api")
public class JsonApiController {

    @Autowired
    FirstTypeService firstTypeService;

    @Autowired
    SecondaryTypeService secondaryTypeService;

    @RequestMapping("type/all")
    @ResponseBody
    public String getAllType() {
        List<Tuple2<FirstType, List<SecondaryType>>> collect = firstTypeService.selectAll()
                .stream()
                .map(firstType -> {
                    Example example = new Example(SecondaryType.class);
                    Example.Criteria criteria = example.createCriteria();
                    criteria.andEqualTo("firstTypeId", firstType.getId());
                    return new Tuple2<>(firstType, secondaryTypeService.selectByCondition(example));

                }).collect(Collectors.toList());

        JSONArray jsonArray = generateTypeJSONObject(collect);
        return jsonArray.toJSONString();
    }


    @RequestMapping("firstType/select/all")
    @ResponseBody
    public String selectFirstTypeSelectAll() {
        List<FirstType> firstTypeList = firstTypeService.selectAll();
        JSONArray array = new JSONArray();
        for (FirstType firstType : firstTypeList) {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("id", firstType.getId());
            jsonObject.put("name", firstType.getName());
            array.add(jsonObject);
        }
        return array.toJSONString();
    }

    @RequestMapping("secondaryType/select/{firstTypeId}")
    @ResponseBody
    public String selectSecondaryTypeByFirstTypeId(@PathVariable Integer firstTypeId){
        Example example = new Example(SecondaryType.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("firstTypeId", firstTypeId);

        List<SecondaryType> secondaryTypes = secondaryTypeService.selectByCondition(example);

        JSONArray array = new JSONArray();

        for (SecondaryType secondaryType : secondaryTypes) {
            JSONObject jsonObject = new JSONObject();

            jsonObject.put("id", secondaryType.getId());
            jsonObject.put("name", secondaryType.getName());

            array.add(jsonObject);
        }

        return array.toJSONString();
    }

    private JSONArray generateTypeJSONObject(List<Tuple2<FirstType, List<SecondaryType>>> collect) {
        JSONArray jsonArray = new JSONArray();

        for (Tuple2<FirstType, List<SecondaryType>> firstTypeListTuple2 : collect) {
            jsonArray.add(convertTupleToJSONObject(firstTypeListTuple2));
        }

        return jsonArray;
    }

    private JSONObject convertTupleToJSONObject(Tuple2<FirstType, List<SecondaryType>> firstTypeListTuple2) {

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("id", firstTypeListTuple2._1().getId());
        jsonObject.put("name", firstTypeListTuple2._1().getName());
        jsonObject.put("secondaryTypeArray", convertSecondaryTypeListToJSONArray(firstTypeListTuple2._2()));


        return jsonObject;
    }

    private JSONArray convertSecondaryTypeListToJSONArray(List<SecondaryType> secondaryTypes) {
        JSONArray jsonArray = new JSONArray();

        for (SecondaryType secondaryType : secondaryTypes) {
            jsonArray.add(convertSecondaryTypeToJSONObject(secondaryType));
        }

        return jsonArray;
    }

    private JSONObject convertSecondaryTypeToJSONObject(SecondaryType secondaryType) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("id", secondaryType.getId());
        jsonObject.put("name", secondaryType.getName());

        return jsonObject;
    }
}
